home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 13 / Game / Bullet.cpp next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  5.4 KB  |  170 lines

  1. //-----------------------------------------------------------------------------
  2. // Bullet.h implementation.
  3. // Refer to the Bullet.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Main.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The bullet class constructor.
  12. //-----------------------------------------------------------------------------
  13. Bullet::Bullet( SceneObject *owner, D3DXVECTOR3 translation, D3DXVECTOR3 direction, float velocity, float range, float damage )
  14. {
  15.     // Store the owner of the bullet.
  16.     m_owner = owner;
  17.  
  18.     // Create a ray intersection result for the bullet.
  19.     m_hitResult = new RayIntersectionResult;
  20.  
  21.     // Indicate that the bullet has not moved yet.
  22.     m_totalDistance = 0.0f;
  23.  
  24.     // The bullet has not expired yet.
  25.     m_expired = false;
  26.  
  27.     // Set all the properties of the bullet.
  28.     m_translation = translation;
  29.     m_direction = direction;
  30.     m_velocity = velocity;
  31.     m_range = range;
  32.     m_damage = damage;
  33. }
  34.  
  35. //-----------------------------------------------------------------------------
  36. // The bullet class destructor.
  37. //-----------------------------------------------------------------------------
  38. Bullet::~Bullet()
  39. {
  40.     SAFE_DELETE( m_hitResult );
  41. }
  42.  
  43. //-----------------------------------------------------------------------------
  44. // Updates the bullet.
  45. //-----------------------------------------------------------------------------
  46. void Bullet::Update( float elapsed )
  47. {
  48.     // Clear the hit result.
  49.     m_hitResult->material = NULL;
  50.     m_hitResult->distance = 0.0f;
  51.     m_hitResult->hitObject = NULL;
  52.  
  53.     // Cast a ray through the scene to see if the bullet might hit something.
  54.     if( g_engine->GetSceneManager()->RayIntersectScene( m_hitResult, m_translation, m_direction, true, m_owner, true ) == true )
  55.     {
  56.         // Ensure the hit distance is within the velocity of the bullet.
  57.         if( m_hitResult->distance <= m_velocity * elapsed )
  58.         {
  59.             // Ensure the bullet will not go beyond its range.
  60.             m_totalDistance += m_hitResult->distance;
  61.             if( m_totalDistance > m_range )
  62.             {
  63.                 // Clear the hit result.
  64.                 m_hitResult->material = NULL;
  65.                 m_hitResult->distance = 0.0f;
  66.                 m_hitResult->hitObject = NULL;
  67.  
  68.                 // Indicate that the bullet has expired.
  69.                 m_expired = true;
  70.  
  71.                 // Return now so that the collision isn't processed.
  72.                 return;
  73.             }
  74.  
  75.             // The bullet has legally hit something, so expire it.
  76.             m_expired = true;
  77.  
  78.             // Only the host is allowed to process bullet collision results.
  79.             if( g_engine->GetNetwork()->IsHost() == false )
  80.                 return;
  81.  
  82.             // Ensure the bullet hit an object, and not just part of the scene.
  83.             if( m_hitResult->hitObject == NULL )
  84.                 return;
  85.  
  86.             // Check if the bullet hit another player.
  87.             if( m_hitResult->hitObject->GetType() == TYPE_PLAYER_OBJECT )
  88.                 ( (PlayerObject*)m_hitResult->hitObject )->Hurt( m_damage, (PlayerObject*)m_owner );
  89.  
  90.             return;
  91.         }
  92.         else
  93.         {
  94.             // Clear the hit result.
  95.             m_hitResult->material = NULL;
  96.             m_hitResult->distance = 0.0f;
  97.             m_hitResult->hitObject = NULL;
  98.         }
  99.     }
  100.  
  101.     // Expire the bullet if it will move beyond its range.
  102.     m_totalDistance += m_velocity * elapsed;
  103.     if( m_totalDistance > m_range )
  104.     {
  105.         m_expired = true;
  106.  
  107.         return;
  108.     }
  109.  
  110.     // Move the bullet.
  111.     m_translation += m_direction * ( m_velocity * elapsed );
  112. }
  113.  
  114. //-----------------------------------------------------------------------------
  115. // Returns true if the bullet is expired.
  116. //-----------------------------------------------------------------------------
  117. bool Bullet::IsExpired()
  118. {
  119.     return m_expired;
  120. }
  121.  
  122. //-----------------------------------------------------------------------------
  123. // The bullet manager class constructor.
  124. //-----------------------------------------------------------------------------
  125. BulletManager::BulletManager()
  126. {
  127.     // Create the linked list of bullets.
  128.     m_bullets = new LinkedList< Bullet >;
  129. }
  130.  
  131. //-----------------------------------------------------------------------------
  132. // The bullet manager class destructor.
  133. //-----------------------------------------------------------------------------
  134. BulletManager::~BulletManager()
  135. {
  136.     SAFE_DELETE( m_bullets );
  137. }
  138.  
  139. //-----------------------------------------------------------------------------
  140. // Allows the bullet manager to update its bullets.
  141. //-----------------------------------------------------------------------------
  142. void BulletManager::Update( float elapsed )
  143. {
  144.     // Go through the list of bullets.
  145.     Bullet *remove = NULL;
  146.     Bullet *bullet = m_bullets->GetFirst();
  147.     while( bullet != NULL )
  148.     {
  149.         // Check if the bullet has expired. If not, allow it to update.
  150.         if( bullet->IsExpired() == true )
  151.             remove = bullet;
  152.         else
  153.             bullet->Update( elapsed );
  154.  
  155.         // Go to the next bullet.
  156.         bullet = m_bullets->GetNext( bullet );
  157.  
  158.         // If the bullet did expire, then remove it now.
  159.         if( remove != NULL )
  160.             m_bullets->Remove( &remove );
  161.     }
  162. }
  163.  
  164. //-----------------------------------------------------------------------------
  165. // Adds a new bullet to the bullet manager.
  166. //-----------------------------------------------------------------------------
  167. void BulletManager::AddBullet( SceneObject *owner, D3DXVECTOR3 translation, D3DXVECTOR3 direction, float velocity, float range, float damage )
  168. {
  169.     m_bullets->Add( new Bullet( owner, translation, direction, velocity, range, damage ) );
  170. }